//
//  BIDBaseCollectionView.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDBaseCollectionView.h"
#import "BIDPullDownRefreshView.h"
#import "BIDLoadMoreDataView.h"

static const NSUInteger MIN_PULL_DOWN_DISTANCE = 100;

@interface BIDBaseCollectionView()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    BIDPullDownRefreshView *_pullDownRefreshView;
    BIDLoadMoreDataView *_loadMoreDataView;
    //下拉刷新
    BOOL _bPullDown;
    //上拉加载更多
    BOOL _bPullUp;
    //是否可以刷新
    BOOL _bCanRefresh;
    //是否还有数据
    BOOL _bHasMoreData;
    //加载中
    BOOL _bLoading;
    //刷新中
    BOOL _bRefreshing;
    //
    BOOL _bDragging;
}
@end

@implementation BIDBaseCollectionView
@synthesize myDelegate;

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initForItselfWithFrame:self.frame];
}

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if(self)
    {
        self.alwaysBounceVertical = YES;
        [self initForItselfWithFrame:frame];
    }
    return self;
}

- (void)initForItselfWithFrame:(CGRect)frame
{
    self.dataSource = self;
    self.delegate = self;
    _bHasMoreData = YES;
    //
    //
    _pullDownRefreshView = (BIDPullDownRefreshView*)[[[NSBundle mainBundle] loadNibNamed:@"BIDPullDownRefreshView" owner:self options:nil] lastObject];
    CGRect pullDownRefreshViewFrame = _pullDownRefreshView.frame;
    pullDownRefreshViewFrame.origin.x = 0;
    pullDownRefreshViewFrame.origin.y = -pullDownRefreshViewFrame.size.height;
    pullDownRefreshViewFrame.size.width = frame.size.width;
    pullDownRefreshViewFrame.size.height = 50;
    _pullDownRefreshView.frame = pullDownRefreshViewFrame;
    [self addSubview:_pullDownRefreshView];
    //
    _loadMoreDataView = (BIDLoadMoreDataView*)[[[NSBundle mainBundle] loadNibNamed:@"BIDLoadMoreDataView" owner:self options:nil] lastObject];
    CGRect loadMoreDataViewFrame = _loadMoreDataView.frame;
    loadMoreDataViewFrame.origin.x = 0;
    loadMoreDataViewFrame.origin.y = 0;
    loadMoreDataViewFrame.size.width = frame.size.width;
    _loadMoreDataView.frame = loadMoreDataViewFrame;
    _loadMoreDataView.hidden = YES;
    [_loadMoreDataView setAttachmentPos:POS_UP];
    [self addSubview:_loadMoreDataView];
    //
}

- (void)clearData
{
}
/**
 *  移除加载更多数据视图
 */
- (void)removeLoadMoreDataView
{
    [_loadMoreDataView removeFromSuperview];
}

/**
 *刷新结束
 */
- (void)endRefresh
{
    _bLoading = NO;
    [_pullDownRefreshView taskEnd];
    [_loadMoreDataView taskEnd];
    [self setContentInset:UIEdgeInsetsZero];
}
/**
 *  没有更多数据了
 */
- (void)hasNoMoreData
{
    _bHasMoreData = NO;
    [_loadMoreDataView hasNoMoreData];
}
- (void)hasMoreData
{
    _bHasMoreData = YES;
}

- (void)setRefreshViewBgColor:(UIColor *)bgColor
{
    _pullDownRefreshView.backgroundColor = bgColor;
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _bDragging = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if(offset.y<=0)
    {
        //下拉
        if(!_bLoading)
        {
            if(fabsf(offset.y) > MIN_PULL_DOWN_DISTANCE)
            {
                [_pullDownRefreshView taskReady];
            }
            else
            {
                [_pullDownRefreshView taskIdle];
            }
        }
        else
        {
            
            [scrollView setContentInset:UIEdgeInsetsMake(CGRectGetHeight(_pullDownRefreshView.frame), 0, 0, 0)];
        }
    }
    else
    {
        //上拉
        if(scrollView.contentSize.height>self.frame.size.height)
        {
            _loadMoreDataView.hidden = NO;
            CGRect frame = _loadMoreDataView.frame;
            frame.origin.y = scrollView.contentSize.height;
            _loadMoreDataView.frame = frame;
        }
        else
        {
            _bHasMoreData = NO;
            return;
        }
        //上拉执行
        if(_bLoading)
        {
            scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, CGRectGetHeight(_loadMoreDataView.frame), 0.0f);
        }
        else if(!_bHasMoreData)
        {
            [_loadMoreDataView hasNoMoreData];
        }
        else if(_bDragging && (scrollView.contentOffset.y+self.frame.size.height-scrollView.contentSize.height)>0)
        {
            if(scrollView.contentOffset.y+self.frame.size.height-scrollView.contentSize.height<MIN_PULL_DOWN_DISTANCE)
            {
                [_loadMoreDataView taskIdle];
            }
            else
            {
                [_loadMoreDataView taskReady];
            }
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    _bDragging = NO;
    CGPoint offset = scrollView.contentOffset;
    if(offset.y<0)
    {
        //下拉刷新
        if(!_bLoading && fabsf(offset.y) > MIN_PULL_DOWN_DISTANCE)
        {
            NSLog(@"_bLoading && fabsf(offest.y)");
            _bLoading = YES;
            _bHasMoreData = YES;
            //可以刷新并且当前不是正在刷新中这个状态
            [_pullDownRefreshView taskStart];
            [myDelegate refresh];
        }
    }
    else
    {
        //上拉加载更多
        if(!_bLoading && _bHasMoreData && (scrollView.contentOffset.y+self.frame.size.height-scrollView.contentSize.height>=MIN_PULL_DOWN_DISTANCE))
        {
            _bLoading = YES;
            [_loadMoreDataView taskStart];
            [myDelegate loadMoreData];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}


#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if([myDelegate respondsToSelector:@selector(numberOfSectionsInCollectionView:)])
    {
        return [myDelegate numberOfSectionsInCollectionView:collectionView];
    }
    else
    {
        return 1;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [myDelegate collectionView:collectionView numberOfItemsInSection:section];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [myDelegate collectionView:collectionView cellForItemAtIndexPath:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if([myDelegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)])
    {
        [myDelegate collectionView:collectionView didSelectItemAtIndexPath:indexPath];
    }
}

@end
